package com.googlecode.agileplugins.cruisewatcher;

import com.intellij.openapi.diagnostic.Logger;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import java.io.File;

public final class WavFilePlayer {
    private Logger logger = Logger.getInstance(getClass().getName());

    public void play(File wavFile) {
        try {
            if (wavFile != null && wavFile.exists()) {
                AudioInputStream stream = AudioSystem.getAudioInputStream(wavFile);
                DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat());
                Clip clip = (Clip) AudioSystem.getLine(info);
                clip.open(stream);
                clip.start();
            }
        } catch (Exception e) {
            logger.info("Can't play " + wavFile + ": " + e);
        }
    }
}
